#include "head.h"

int make_nonblock(int fd) {
    int flag;
    if ((flag = fcntl(fd, F_GETFL)) < 0) {
        perror("fcntl");
        return -1;
    }
    flag |= O_NONBLOCK;
    return fcntl(fd, F_SETFL, flag);
}

int make_block(int fd) {
    int flag;
    if ((flag = fcntl(fd, F_GETFL)) < 0) {
        perror("fcntl");
        return -1;
    }
    flag &= ~O_NONBLOCK;
    return fcntl(fd, F_SETFL, flag);
}

int isRead(int fd) {
    int flag;
    if ((flag = fcntl(fd, F_GETFL)) < 0) {
        perror("fcntl");
        return -1;
    }
    int accessMode = flag & O_ACCMODE;
    if (accessMode == O_RDONLY || accessMode == O_RDWR) {
        return 1;
    }
    return 0;
}

int main(int argc, char **argv) {
    int age;
    if (argv[1] && strcmp(argv[1], "1") == 0) {
        make_nonblock(STDIN_FILENO);
    }
    scanf("%d", &age);
    printf("wyf is %d years old\n", age);
    if (isRead(STDIN_FILENO) == 1) {
        printf("stdint is readable\n");
    } else {
        printf("stdin is not readalbe\n");
    }

    return 0;
}